package com.tomek.ktoryoperator.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.tomek.ktoryoperator.C;

public class PreferenceUtils {
	public static final String PREFERENCE_NAME = "USER_PREFERENCES";

	public static final String prefs_update_service_KEY = "prefs_update_service";
	public static final boolean prefs_update_service_DEFAULT = false;

	public static final String prefs_notifications_new_KEY = "prefs_notifications_new";
	public static final boolean prefs_notifications_new_DEFAULT = true;

	public static final String prefs_notifications_change_KEY = "prefs_notifications_change";
	public static final boolean prefs_notifications_change_DEFAULT = true;

	public static final String prefs_show_photos_KEY = "prefs_show_photos";
	public static final boolean prefs_show_photos_DEFAULT = true;

	public static final String prefs_sorting_KEY = "prefs_sorting";
	public static final int prefs_sorting_DEFAULT = 1;

	public static final int SORTING_DISPLAY_NAME = 1;
	public static final int SORTING_LAST_NAME = 2;
	public static final int SORTING_PHONE_NUMBER = 3;

	// NOT EDITABLE

	public static final long prefs_new_service_interval_DEFAULT = 5 * C.MINUTE_IN_MILLIS;
	public static final int prefs_new_service_simu_reads_DEFAULT = 1;

	public static final String prefs_update_service_interval_KEY = "prefs_update_service_interval";
	public static final long prefs_update_service_interval_DEFAULT = C.WEEK_IN_MILLIS;

	

	private static SharedPreferences getPrefs(Context context) {
		return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
	}

	public static int getSorting(Context context) {
		return getInt(context, prefs_sorting_KEY, prefs_sorting_DEFAULT);
	}

	public static boolean showPhotos(Context context) {
		return getPrefs(context).getBoolean(prefs_show_photos_KEY, prefs_show_photos_DEFAULT);
	}

	public static boolean shouldRunInBackground(Context context) {
		return getPrefs(context).getBoolean(prefs_update_service_KEY, prefs_update_service_DEFAULT);
	}

	public static boolean notifyAboutFirstRead(Context context) {
		return getPrefs(context).getBoolean(prefs_notifications_new_KEY, prefs_notifications_new_DEFAULT);
	}

	public static boolean notifyAboutNetworkOperatorChange(Context context) {
		return getPrefs(context).getBoolean(prefs_notifications_change_KEY, prefs_notifications_change_DEFAULT);
	}

	public static int getNumberToReadCount(Context context) {
		return prefs_new_service_simu_reads_DEFAULT;
	}

	public static long getReadInterval(Context context) {
		return prefs_new_service_interval_DEFAULT;
	}

	private static int getInt(Context context, String key, int defaultValue) {
		try {
			return Integer.parseInt(getPrefs(context).getString(key, ""));
		} catch (NumberFormatException e) {
			return defaultValue;
		}
	}
}